package com.health.work.activity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 托管页面
 * 
 * @author yinxing
 * 
 */
public class TrusteeShipActivity extends Activity implements OnClickListener {

	private Button back;
	private Button stopButton;
	private final static int RESUCCESS = -1;
	private final static int REFAILE = -2;
	private final static int SUCCESS = 0;
	private final static int WAIT = 3;
	private final static int NODATA = 4;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private TextView expName;
	private TextView state;
	private Thread thread;
	private String mobilePhoneNum;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				break;
			case FAILE:
				TipsUtil.showAlert(TrusteeShipActivity.this, null, "数据请求失败！");
				break;
			case ERROR:
				TipsUtil.showAlert(TrusteeShipActivity.this, null, "未知错误！");
				break;
			case RESUCCESS:
				severSuccess();
				expName.setText("尚未托管");
				Toast.makeText(TrusteeShipActivity.this, "托管解除成功",
						Toast.LENGTH_SHORT).show();

				break;
			case REFAILE:
				Toast.makeText(TrusteeShipActivity.this, "托管解除失败",
						Toast.LENGTH_SHORT).show();
				break;
			case NODATA:
				expName.setText("尚未托管");
				Toast.makeText(TrusteeShipActivity.this, "尚未托管",
						Toast.LENGTH_SHORT).show();
				break;
			case WAIT:
				expName.setVisibility(View.GONE);
				state.setVisibility(View.VISIBLE);
				Toast.makeText(TrusteeShipActivity.this, "待专家确认",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.trusteeship_layout);
		mobilePhoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		initPages();
		checkTrusteeShip();
	}

	private void initPages() {
		// TODO Auto-generated method stub
		back = (Button) findViewById(R.id.btnBack);
		stopButton = (Button) findViewById(R.id.btnUp);
		expName = (TextView) findViewById(R.id.exp_name);
		back.setOnClickListener(this);
		stopButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnBack:
			finish();
			break;
		case R.id.btnUp:
			getData();
			break;
		default:
			break;
		}
	}

	private void checkTrusteeShip() {
		final String phoneNum = UserInfoUtils.getUserProperty(this,
				HEALTHPASSPORT.MOBILEPHONE);
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetTrusteeship(phoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								Log.e("GetTrusteeshipStr", result);
								if ("0".equals(result)) {// 尚未托管
									handler.sendEmptyMessage(NODATA);
								} else if ("1".equals(result)) {// 待专家确认
									handler.sendEmptyMessage(WAIT);
								} else {// 托管成功

								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub
								if (object != null && !"".equals(object)) {// 托管成功
									handler.sendEmptyMessage(SUCCESS);
								}
							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub
							}
						});
			}
		});
		thread.start();
	}

	/**
	 * 解除托管
	 */
	private void getData() {

		thread = new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().RelieveTrusteeship(mobilePhoneNum,
						new DefaultJSONData() {

							@Override
							public void parse(String result)
									throws JSONException {
								// TODO Auto-generated method stub
								if ("0".equals(result)) {
									handler.sendEmptyMessage(RESUCCESS);
								} else if ("1".equals(result)) {
									handler.sendEmptyMessage(REFAILE);
								} else {
									handler.sendEmptyMessage(REFAILE);
								}
							}

							@Override
							public void parse(JSONObject object)
									throws JSONException {
								// TODO Auto-generated method stub

							}

							@Override
							public void parse(JSONArray array)
									throws JSONException {
								// TODO Auto-generated method stub

							}
						});
			}
		});
		thread.start();
	}

	private void severSuccess() {

	}
}
